﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.Serialization;
using System.IO;
using helper = CommonT4.HelperExtensionT4;

namespace WCFServiceT4
{
	class Run
	{

		static void Main(string[] args)
		{
			
			var settings = WCFServiceT4.Properties.Settings.Default;
			var excludes = settings.ExcludeTypeFullNameSubstrings.Cast<string>().ToList();
			var srcAssembly = Assembly.LoadFile(settings.SrcAssembly);
			var servicetemplate = new WCFServiceT4.ServiceTemplate
			{
				SrcAssembly = srcAssembly, 				
				ServiceNamespace = settings.ServiceNamespace,
				ServiceName = settings.ServiceName,
				MustExcludeSrcTypes = excludes,
				RequiredBaseTypes = settings.RequiredBaseTypes.Cast<string>()
			};
			var clientaccesspolicyTemplate = new IClientAccessPolicyTemplate
			{
				ServiceNamespace = settings.ServiceNamespace
			};

			string servicecontent = servicetemplate.TransformText();			
			
			string clientaccesspolicycontent = clientaccesspolicyTemplate.TransformText();
			foreach (var dir in settings.outputDirectories.Cast<string>())
			{
				FileInfo f = new FileInfo(dir);
				if (f.Exists)
					f.IsReadOnly = false;
				string servicecontractPath = Path.Combine(dir, "I" + settings.ServiceName + ".cs");				
				string clientAccessPath = Path.Combine(dir, "IClientAccessPolicy.cs");
				System.IO.File.WriteAllText(servicecontractPath, servicecontent);
				System.IO.File.WriteAllText(clientAccessPath, clientaccesspolicycontent);
			}

		}



		
        
		
	}
}
